package net.begincode.service.chapter.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import utils.CacheUtils;
import config.DbTemplate;
import net.begincode.bean.BcCode;
import net.begincode.bean.CourseChapter;
import net.begincode.service.chapter.ChapterService;
import net.begincode.service.code.impl.CodeServiceImpl;

public class ChapterServiceImpl implements ChapterService {
	private ChapterServiceImpl() {
	}

	private static ChapterServiceImpl instance = null;

	public static ChapterServiceImpl getInstance() {
		if (instance == null) {
			synchronized (ChapterServiceImpl.class) {
				if (instance == null) {
					instance = new ChapterServiceImpl();
				}
			}
		}
		return instance;
	}
	
	@Override
	public List<CourseChapter> selNewChapterLimitN(int courseId,int n) {
		
		String selSql = " select * from  course_chapter where bc_course_id = ? order by cdate desc limit ? ";
		return DbTemplate.queryForList(selSql, new Object[]{courseId,n}, CourseChapter.class);
	}
	@Override
	public int addChapter(CourseChapter chapter) {
		
		StringBuilder addSql = new StringBuilder();
		addSql.append(" insert into course_chapter ( chapter_title , cha_abstract , chapter_content , bc_course_id  , praise , view_count , chapter_keyword ,writer , chapter_weight , discuss_flag  ,chapter_resouce ,cdate, check_flag,bc_user_id )");
		addSql.append(" values (?,?,?,?,?,?,?,?,?,?,?,now(),'0',?) ");
		return DbTemplate.insert(addSql.toString(), new Object[]{chapter.getChapter_title(),chapter.getCha_abstract(),chapter.getChapter_content()
			,chapter.getBc_course_id(),chapter.getPraise(),chapter.getView_count(),chapter.getChapter_keyword(),chapter.getWriter(),chapter.getChapter_weight(),chapter.getDiscuss_flag(),chapter.getChapter_resouce(),chapter.getBc_user_id()});
	}

	@Override
	public CourseChapter selChapter(String id) {
		String selSql = " select * from course_chapter where course_chapter_id = ? ";
		return DbTemplate.queryForObject(selSql, new Object[]{id}, CourseChapter.class);
	}

	@Override
	public List<CourseChapter> selSameChapterLimitFiveCache(int courseId,int chapterWeight) {
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.CHAPTER_CACHE, courseId+"capterLimitFive");
		if(dbEle != null){
			return (List<CourseChapter>)dbEle;
		}else{
			String maxSql = " select * from course_chapter where bc_course_id = ? and chapter_weight > ?  order by chapter_weight  limit ? ";
			List<CourseChapter> maxList = DbTemplate.queryForList(maxSql, new Object[]{courseId,chapterWeight,5}, CourseChapter.class);
			if(maxList.size() < 5){
				String minSql = " select * from course_chapter where bc_course_id = ? and chapter_weight < ?  order by chapter_weight desc limit ? ";
				maxList.addAll(DbTemplate.queryForList(minSql, new Object[]{courseId,chapterWeight,5-maxList.size()},CourseChapter.class ));
			}
			CacheUtils.addEleByParam(CacheUtils.CHAPTER_CACHE, courseId+"capterLimitFive", maxList);
			return maxList;
		}
	}

	@Override
	public List<CourseChapter> selNewChapterByCodeId(String codeId, int n) {
		
		String selSql = "select cc.* from  chapter_code_rel ccr left join  course_chapter cc on cc.course_chapter_id = ccr.course_chapter_id where ccr.bc_code_id = ? limit ? ";
		return DbTemplate.queryForList(selSql, new Object[]{codeId,n}, CourseChapter.class);

	}

	@Override
	public List<CourseChapter> selChapterByCouseId(String courseId) {
		
		String selSql = " select * from course_chapter where bc_course_id = ?  order by chapter_weight  ";
		return DbTemplate.queryForList(selSql, new Object[]{courseId}, CourseChapter.class);
	}

	@Override
	public List<CourseChapter> selChapterByParam(Map param) {
		
		String selSql = " select * from course_chapter ";
		return DbTemplate.queryForList(selSql, new Object[]{}, CourseChapter.class);
	}

	@Override
	public int updChaper(CourseChapter courseChapter) {
		List paraList = new ArrayList();
		StringBuilder updSql  = new StringBuilder();
		updSql.append(" update course_chapter set chapter_title = ? ");
		paraList.add(courseChapter.getChapter_title());
		if(courseChapter.getCha_abstract() != null){
			updSql.append(" ,cha_abstract = ? ");
			paraList.add(courseChapter.getCha_abstract());
		}
		if(courseChapter.getChapter_content() != null){
			updSql.append(" ,chapter_content = ? ");
			paraList.add(courseChapter.getChapter_content());
		}
		if(courseChapter.getBc_course_id() != 0){
			updSql.append(" ,bc_course_id = ? ");
			paraList.add(courseChapter.getBc_course_id());
		}
		if(courseChapter.getPraise() != 0){
			updSql.append(" ,praise = ? ");
			paraList.add(courseChapter.getPraise());
		}
		if(courseChapter.getView_count() != 0){
			updSql.append(" ,view_count = ? ");
			paraList.add(courseChapter.getView_count());
		}
		if(courseChapter.getChapter_keyword() != null){
			updSql.append(" ,chapter_keyword = ? ");
			paraList.add(courseChapter.getChapter_keyword());
		}
		if(courseChapter.getWriter() != null){
			updSql.append(" ,writer = ? ");
			paraList.add(courseChapter.getWriter());
		}
		if(courseChapter.getChapter_weight() != 0){
			updSql.append(" ,chapter_weight = ? ");
			paraList.add(courseChapter.getChapter_weight());
		}
		if(courseChapter.getDiscuss_flag() != null){
			updSql.append(" ,discuss_flag = ? ");
			paraList.add(courseChapter.getDiscuss_flag());
		}
		if(courseChapter.getChapter_resouce() != null){
			updSql.append(" ,chapter_resouce = ? ");
			paraList.add(courseChapter.getChapter_resouce());
		}
		if(courseChapter.getCheck_flag() != null){
			updSql.append(" ,check_flag = ? ");
			paraList.add(courseChapter.getCheck_flag());
		}
		if(courseChapter.getBc_user_id()!= 0){
			updSql.append(" ,bc_user_id = ? ");
			paraList.add(courseChapter.getBc_user_id());
		}
		updSql.append(" where course_chapter_id = ? ");
		paraList.add(courseChapter.getCourse_chapter_id());
		return DbTemplate.update(updSql.toString(), paraList.toArray());
	}

	@Override
	public CourseChapter selChapterCache(String id) {
		
		String selSql = " select * from course_chapter where course_chapter_id = ? ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.CHAPTER_CACHE, "chapterId"+id);
		if(dbEle != null){
			return (CourseChapter)dbEle;
		}else{
			CourseChapter ccs = DbTemplate.queryForObject(selSql, new Object[]{id}, CourseChapter.class);
			CacheUtils.addEleByParam(CacheUtils.CHAPTER_CACHE, "chapterId"+id, ccs);
			return ccs;
		}
	}

	@Override
	public List<CourseChapter> selNewChapterByCodeIdCache(String codeId, int n) {
		
		String selSql = "select cc.* from  chapter_code_rel ccr left join  course_chapter cc on cc.course_chapter_id = ccr.course_chapter_id where ccr.bc_code_id = ? limit ? ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.CHAPTER_CACHE, "newChapterRelCode"+codeId+n);
		if(dbEle != null){
			return (List<CourseChapter>)dbEle;
		}else{
			List<CourseChapter> ccs = DbTemplate.queryForList(selSql, new Object[]{codeId,n}, CourseChapter.class);
			CacheUtils.addEleByParam(CacheUtils.CHAPTER_CACHE, "newChapterRelCode"+codeId+n, ccs);
			return ccs;
		}
	}

	@Override
	public List<CourseChapter> selChapterByCouseIdCache(String courseId) {
		
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.CHAPTER_CACHE, "chapterlistByCId"+courseId);
		if(dbEle != null){
			return (List<CourseChapter>)dbEle;
		}else{
			String selSql = " select * from course_chapter where bc_course_id = ?  order by chapter_weight  ";
			List<CourseChapter> ccs = DbTemplate.queryForList(selSql, new Object[]{courseId}, CourseChapter.class);
			CacheUtils.addEleByParam(CacheUtils.CHAPTER_CACHE, "chapterlistByCId"+courseId, ccs);
			return ccs;
		}
	}

	@Override
	public void updChapterViewCount(String id, int count) {
		String updSql = " update course_chapter set view_count = view_count + ? where course_chapter_id = ?  ";
		DbTemplate.update(updSql, new Object[]{count,id});
	}
}
